<?php


namespace app\wechat\controller;


use app\BaseController;
use app\common\model\Order;
use app\common\model\Finance;
use app\common\model\UserBook;
use Psr\Cache\CacheException;
use think\cache\driver\Redis;
use think\facade\Cache;
use think\facade\Db;
use think\exception\ValidateException;
class Teacher extends AuthBase
{
    /**
     * 老师首页
     */
    public function index(){
        $user=Db::name('user')
            ->where('id',$this->userId)
            ->field('id,nickname,head_img,major_one,major_two,major_three')
            ->find();
        //我的待办
        $model['one'] = Db::name('order')->where(['teacher_id'=>$this->userId,'status'=>2])->count();
        $model['two'] = Db::name('order')->where(['teacher_id'=>$this->userId,'status'=>[3,4,5,6]])->count();
        $model['three'] = Db::name('order')->where(['teacher_id'=>$this->userId,'status'=>[7]])->count();
        //关键数据
        $model['total_order_price_gong']=Db::name('order')
            ->where(['teacher_id'=>$this->userId,'type'=>1])
            ->where('status','<>',10)
            ->sum('teacher_price');
        $model['total_order_price_wen']=Db::name('order')
            ->where(['teacher_id'=>$this->userId,'type'=>2])
            ->where('status','<>',10)
            ->sum('teacher_price');
        $model['order_num_gong']=Db::name('order')
            ->where(['teacher_id'=>$this->userId,'type'=>1])
            ->where('status','<>',10)
            ->count();
        $model['order_num_wen']=Db::name('order')
            ->where(['teacher_id'=>$this->userId,'type'=>2])
            ->where('status','<>',10)
            ->count();
        $order_id_gong=Db::name('order')
            ->where(['teacher_id'=>$this->userId,'type'=>1])
            ->where('status','<>',10)
            ->column('id');
        $order_id_wen=Db::name('order')
            ->where(['teacher_id'=>$this->userId,'type'=>2])
            ->where('status','<>',10)
            ->column('id');
        $model['yet_price_gong']=Db::name('finance')
            ->where(['order_id'=>$order_id_gong,'type'=>3,'status'=>4])
            ->sum('price');
        $model['yet_price_wen']=Db::name('finance')
            ->where(['order_id'=>$order_id_wen,'type'=>3,'status'=>4])
            ->sum('price');
        $data['user']=$user;
        $data['stat']=$model;
        return $this->show(1,'',$data);
    }
    /**\
     * @return \think\response\Json
     * 订单列表
     */
    public function order_index()
    {
        $type = input('get.type');
        $page = input('param.page', 1, 'intval');
        $limit = input('param.limit', 20, 'intval');
//        $search = input('param.search');
//        $where=[];
//        if($search){
//            $where[] = [
//                'o.order_no|u.nickname', 'like',  "%".$search."%"
//            ];
//        }
        if ($type){
            $where[] = [
                'type', '=',  $type
            ];
        }
        $where[]=[
            'status',"in",[2,3,4,5,6,7,8,9]
        ];
        $where[]=[
            'teacher_id','=',$this->userId
        ];
        $model = Order::where($where)
            ->page($page, $limit)
            ->column('id,name,teacher_price,teacher_yet_price,status,type');
        $count = Order::where($where)
            ->count();
        $data['total'] = $count;
        $data['list'] = $model;
        $data['page'] = $page;
        return $this->show(1, '', $data);
    }
    /**
     * 订单市场
     */
    public function order_mark(){
        $page = input('param.page', 1, 'intval');
        $limit = input('param.limit', 20, 'intval');
        $where[]=[
            'status',"=",1
        ];
        $model = Order::where($where)
            ->page($page, $limit)
            ->column('id,name,order_no,type,software_ask_one,software_ask_two,software_ask_three');
        $count = Order::where($where)
            ->count();
        $data['total'] = $count;
        $data['list'] = $model;
        $data['page'] = $page;
        return $this->show(1, '', $data);
    }
    /**\
     * @return \think\response\Json
     * 待处理订单列表
     */
    public function await_order()
    {
        $type=input('param.type');
        $status=input('param.status');
        $page = input('param.page', 1, 'intval');
        $limit = input('param.limit', 20, 'intval');
        if ($status==1){
            $where[]=[
                'status',"=",2
            ];
        }elseif($status==2){
            $where[]=[
                'status','in',[3,4,5,6]
            ];
        }elseif($status==3){
            $where[]=[
                'status','in',[7]
            ];
        }
        if ($type){
            $where[]=[
                'type','=',$type
            ];
        }
        $where[]=[
            'teacher_id','=',$this->userId
        ];
        $model = Order::where($where)
            ->page($page, $limit)
            ->column('id,name,order_no,type,software_ask_one,software_ask_two,software_ask_three,clock_date,status');
        foreach ($model as $k=>$v){
            if($v['clock_date']){
                $model[$k]['clock_date'] = date('Y-m-d',$v['clock_date']);
                if($v['clock_date']<time()){
                    $v[$k]['biaohong']=1;
                }
            }else{
                $model[$k]['biaohong'] = 2;
            }
            //查找有多少条消息
            if ($v['status']==2){
                $model[$k]['message_num']=2;
            }else{
                if ($v['status']==3){
                    $status=2;
                }elseif($v['status']==4){
                    $status=3;
                }elseif($v['status']==5){
                    $status=4;
                }elseif($v['status']==6){
                    $status=5;
                }else{
                    $status=6;
                }
                $model[$k]['message_num']=Db::name('message')
                    ->where(['order_id'=>$v['id'],'status'=>2,'type'=>1,'order_status'=>$status])
                    ->value('count');
            }
        }
        $count = Order::where($where)
            ->count();
        $data['total'] = $count;
        $data['list'] = $model;
        $data['page'] = $page;
        return $this->show(1, '', $data);
    }
    /**
     * 订单消息
     */
    public function order_message(){
        $id=input('param.id');
        $order=Db::name('order')
            ->where('id',$id)
            ->find();
        if($order['status']==2){
            //如果是2已派单老师就看学生的1
            $status=1;
        }elseif($order['status']==3){
            //如果是3初稿了已经,那就看2修改1
            $status=2;
        }elseif($order['status']==4){
            //如果是4修改1,那就看3修改2
            $status=3;
        }elseif($order['status']==5){
            //如果是5修改2.那就看4,修改3
            $status=4;
        }elseif($order['status']==6){
            //如果是5修改3,那就看5,修改4
            $status=5;
        }elseif($order['status']==7){
            //如果是7修改4,那就看6降重复的
            $status=6;
        }
        $message=Db::name('message')
            ->where(['order_id'=>$id,'order_status'=>$status,'status'=>2,'type'=>1])
            ->find();
        $message['content']=json_decode($message['content'],true);
        $data['order']=$order;
        $data['message']=$message;
        return $this->show(1,'',$data);

    }
    /**
     * 回复消息
     */
    public function return_message(){
        $data=input("post.");
        $order=Db::name('order')
            ->where('id',$data['order_id'])
            ->find();
        if($order['status']==2){
            $order_status=1;
        }elseif($order['status']==3){
            $order_status=2;
        }elseif($order['status']==4){
            //修改1的状态
            $order_status=3;
        }
        elseif($order['status']==5){
            //修改1的状态
            $order_status=4;
        }
        elseif($order['status']==6){
            //修改1的状态
            $order_status=5;
        }
        elseif($order['status']==7){
            //修改1的状态
            $order_status=6;
        }
        $message=Db::name('message')
            ->where(['order_id'=>$order['id'],'order_status'=>$order_status,'type'=>2,'teacher_id'=>$this->userId])
            ->find();
        if($message){
            return show(0,'请勿重复发送消息');
        }
        $result['student_id']=$order['student_id'];
        $result['teacher_id']=$this->userId;
        $result['content']=json_encode($data['content']);
        $result['order_id']=$order['id'];
        $result['status']=1;
        $result['order_status']= $order_status;
        $result['type']=2;
        $result['create_time']=time();
        $result['update_time']=time();
        $result['count']=count($data['content']);
        $res=Db::name('message')
            ->insert($result);
        if($res){
            return show(1,'发送成功');
        }
        return show(0,'发送失败');
    }
}